﻿@model BussinessLogic.Models.QuestionnaireMaintenance
@{
    ViewBag.Title = "Questionnaire";
    int questionIndex = 0;
    int answerIndex = 0;
    string questionId;
    string answerId;
    string freeTextId;
    string multipleChoiseId;
    string aListId;
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>@Model.Name</legend>
        @Html.HiddenFor(model => model.ID)
        <div style="display: none">
            <ol>
                <li id="clone_question">
                    @Html.TextBox("qText", "New Question", new { style = "width:650px" })
                    <img src="../../Content/images/delete.png" alt="" />
                    <ul style="list-style: none">
                        <li>
                            @Html.CheckBox("clone_freeText")
                            @Html.Label("clone_freeText", "Free text question")
                        </li>
                        <li>
                            @Html.CheckBox("clone_multipleChoise")
                            @Html.Label("clone_multipleChoise", "Multiple choise question")
                        </li>
                    </ul>
                    <ul id="clone_aList">
                        <li id="clone_answer">
                            @Html.TextBox("qAnswer", "New Answer", new { style = "width:450px" })
                            @Html.TextBox("qAValue", "0", new { style = "width:20px" })
                            <img src="../../Content/images/delete.png" alt="" style="display: none" />
                        </li>
                    </ul>
                    <input id="btnNewAnswer" type="button" value="New Answer" />
                </li>
            </ol>
        </div>
        <ol id="qList">
            @foreach (var question in Model.Questions)
            {
                questionId = "question" + questionIndex;
                <li id="@questionId">
                    @Html.TextBox("qText", question.Text, new { style = "width:650px" })
                    @if (Model.Questions.Count == 1)
                    {
                        <img src="../../Content/images/delete.png" alt="" onclick="deleteQuestion('@questionId')"
                            style="display: none" />
                    }
                    else
                    {
                        <img src="../../Content/images/delete.png" alt="" onclick="deleteQuestion('@questionId')" />
                    }
                    <ul style="list-style: none">
                        <li>
                            @{freeTextId = "freeText" + questionIndex;}
                            @Html.CheckBox(freeTextId, question.HasFreeAnswer, new { onclick = string.Format("freeTextChanged(this.checked,{0})", questionIndex) })
                            @Html.Label(freeTextId, "Free text question")
                        </li>
                        <li>
                            @{multipleChoiseId = "multipleChoise" + questionIndex;}
                            @if (!question.HasFreeAnswer)
                            {
                                @Html.CheckBox(multipleChoiseId, question.HasMultipleChoice)
                            }
                            else
                            {
                                @Html.CheckBox(multipleChoiseId, question.HasMultipleChoice, new { disabled = true })
                            }
                            @Html.Label(multipleChoiseId, "Multiple choise question")
                        </li>
                    </ul>
                    @if (!question.HasFreeAnswer)
                    {
                        answerIndex = 0;
                        aListId = "aList" + questionIndex;
                        <ul id="@aListId">
                            @foreach (var answer in question.Answers)
                            {
                                answerId = "answer" + answerIndex++;
                                <li id="@answerId">
                                    @Html.TextBox("qAnswer", answer.Text, new { style = "width:450px" })
                                    @Html.TextBox("qAValue", answer.Value, new { style = "width:20px" })
                                    @if (question.Answers.Count == 1)
                                    {
                                        <img src="../../Content/images/delete.png" alt="" onclick="deleteAnswer('@aListId','@answerId')"
                                            style="display: none" />
                                    }
                                    else
                                    {
                                        <img src="../../Content/images/delete.png" alt="" onclick="deleteAnswer('@aListId','@answerId')" />
                                    }
                                </li>
                            }
                        </ul>
                        <input id="btnNewAnswer" type="button" name="btnNewAnswer" value="New Answer" onclick="newAnswer(@questionIndex)" />
                    }
                    else
                    {
                        answerIndex = 0;
                        aListId = "aList" + questionIndex;
                        <ul id="@aListId" style="display:none">
                            @{answerId = "answer" + answerIndex++;}
                            <li id="@answerId">
                                @Html.TextBox("qAnswer", "New Answer", new { style = "width:450px" })
                                @Html.TextBox("qAValue", "0", new { style = "width:20px" })
                                <img src="../../Content/images/delete.png" alt="" onclick="deleteAnswer('@aListId','@answerId')"
                                            style="display: none" />
                            </li>
                        </ul>
                        <input id="btnNewAnswer" name="btnNewAnswer" type="button" value="New Answer" onclick="newAnswer(@questionIndex)"  style="display: none"/>
                    }
                </li>
                    questionIndex++;
            }
        </ol>
        <p>
            <input id="btnNewQuestion" name="btnNewQuestion" type="button" value="New Question"
                onclick="newQuestion()" />
        </p>
    </fieldset>
    <input id="jsonResult" type="text" name="jsonResult" style="display: none" />
    <br />
    <br />
            if (!Model.IsRunning)
            {
    <input type="submit" name="Save" value="Save" />    
            }
            else
            {
    <script type="text/javascript">
        $(document).ready(function () {
            $('[name="btnNewQuestion"]').each(function () {
                $(this).hide();
            });
            $('[name="btnNewAnswer"]').each(function () {
                $(this).hide();
            });
            $('[src="../../Content/images/delete.png"]').each(function () {
                $(this).hide();
            });
        });
    </script>
            }

}
<script src="../../Scripts/Questionnaire/Questionnaire.js" type="text/javascript"></script>
